package com.ruoyi.DataManage.service.impl;

import java.util.List;

import com.ruoyi.DataManage.domain.Category;
import com.ruoyi.DataManage.domain.University;
import com.ruoyi.DataManage.mapper.CategoryMapper;
import com.ruoyi.DataManage.mapper.UniversityMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.DataManage.mapper.Subject_treeMapper;
import com.ruoyi.DataManage.domain.Subject_tree;
import com.ruoyi.DataManage.service.ISubject_treeService;

/**
 * 学科专业统计Service业务层处理
 * 
 * @author CehnFneg
 * @date 2024-04-22
 */
@Service
public class Subject_treeServiceImpl implements ISubject_treeService 
{
    @Autowired
    private Subject_treeMapper subject_treeMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private UniversityMapper universityMapper;

    /**
     * 查询学科专业统计
     * 
     * @param id 学科专业统计主键
     * @return 学科专业统计
     */
    @Override
    public Subject_tree selectSubject_treeById(Long id)
    {
        return subject_treeMapper.selectSubject_treeById(id);
    }

    /**
     * 查询学科专业统计列表
     * 
     * @param subject_tree 学科专业统计
     * @return 学科专业统计
     */
    @Override
    public List<Subject_tree> selectSubject_treeList(Subject_tree subject_tree)
    {
        return subject_treeMapper.selectSubject_treeList(subject_tree);
    }

    /**
     * 新增学科专业统计
     * 
     * @param subject_tree 学科专业统计
     * @return 结果
     */
    @Override
    public int insertSubject_tree(Subject_tree subject_tree)
    {
        if(subject_tree.getType() != 1){
            //插入学校名称
            Long universityId = subject_tree.getUniversityId();
            University university = universityMapper.selectUniversityById(universityId);
            subject_tree.setUniversityName(university.getName());
            //插入门类名称
            Long categoryId = subject_tree.getCategoryId();
            Category category = categoryMapper.selectCategoryById(categoryId);
            subject_tree.setCategoryName(category.getName());
        }


        subject_tree.setCreateTime(DateUtils.getNowDate());
        return subject_treeMapper.insertSubject_tree(subject_tree);
    }

    /**
     * 修改学科专业统计
     * 
     * @param subject_tree 学科专业统计
     * @return 结果
     */
    @Override
    public int updateSubject_tree(Subject_tree subject_tree)
    {
        subject_tree.setUpdateTime(DateUtils.getNowDate());
        return subject_treeMapper.updateSubject_tree(subject_tree);
    }

    /**
     * 批量删除学科专业统计
     *
     * @param ids 需要删除的学科专业统计主键
     * @return 结果
     */
    @Override
    public int deleteSubject_treeByIds(Long[] ids)
    {
        long id = ids[0];
        Long[] childids = subject_treeMapper.selectSubject_treeListByParentId(id);
        if(childids!=null && childids.length != 0){
            subject_treeMapper.deleteSubject_treeByIds(childids);
        }
        return subject_treeMapper.deleteSubject_treeByIds(ids);
    }

    /**
     * 删除学科专业统计信息
     * 
     * @param id 学科专业统计主键
     * @return 结果
     */
    @Override
    public int deleteSubject_treeById(Long id)
    {
        return subject_treeMapper.deleteSubject_treeById(id);
    }

    @Override
    public List<Subject_tree> getMajorList(Long universityId) {
        return subject_treeMapper.getMajorListByUniversityId(universityId);
    }

    @Override
    public List<Subject_tree> getSubjectList(Long categoryId) {
        //-1代表获取全部学科
        if(categoryId != -1){
            /*Category category = categoryMapper.selectCategoryById(categoryId);
            String categoryName = category.getName();
            List<Long> categoryIds = categoryMapper.selectCategoryIdsByName(categoryName);*/
            return subject_treeMapper.getSubjectListByCategoryId(categoryId);
        }
        return subject_treeMapper.getAllSubjectList();
    }

    @Override
    public List<Subject_tree> getMajorListBySubjectId(Long subjectId) {
/*        Subject_tree subject = subject_treeMapper.selectSubject_treeById(subjectId);
        String subjectName = subject.getName();
        List<Long> Ids = subject_treeMapper.selectIdsByName(subjectName);*/
        return subject_treeMapper.getMajorListByParentId(subjectId);
    }
}
